package spring.test2;

import com.blackhorse.domain.AccountAnno;
import com.blackhorse.service.AccountService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * spring ioc注解配置测试
 */
public class Demo1_IOCwithAnnotation {
    ClassPathXmlApplicationContext applicationContext;

    @Before
    public void setUp() {
        String xmlPath = "ioc/applicationContext_annotation.xml";
        //1.创建spring核心容器
        applicationContext = new ClassPathXmlApplicationContext(xmlPath);
    }

    @Test
    public void testCreateBean() {
        AccountAnno account = applicationContext.getBean("accountAnno", AccountAnno.class);
        System.out.println(account);
    }

    @Test
    public void testAutoWried() {
        AccountService service = applicationContext.getBean("accountServiceImplAnno", AccountService.class);
        System.out.println(service);
        service.saveAccount();

    }

    @Test
    public void testScope() {
        for (int i = 0; i < 5; i++) {
            AccountAnno account = applicationContext.getBean("accountAnno", AccountAnno.class);
            System.out.println("hashcode=" + account.hashCode());
        }
    }

    @Test
    public void testLifeScope() {
        AccountAnno account = applicationContext.getBean("accountAnno", AccountAnno.class);
        System.out.println(account.getClass().getName() + "存活...");
    }

    @After
    public void finish() {
        applicationContext.close();
    }
}
